package cn.tedu._05mvcboot03.controller;

import cn.tedu._05mvcboot03.base.response.JsonResult;
import cn.tedu._05mvcboot03.base.response.StatusCode;
import cn.tedu._05mvcboot03.mapper.UserMapper;
import cn.tedu._05mvcboot03.pojo.dto.UserLoginDTO;
import cn.tedu._05mvcboot03.pojo.entity.User;
import cn.tedu._05mvcboot03.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;


@Slf4j
@Api(tags = "用户功能")
@RestController
@RequestMapping("/v1/users/login/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @ApiOperation(value = "登录模块")
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO , @ApiIgnore HttpSession session){
        log.debug("userdto="+userLoginDTO);
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();
        UserVO userVO = userMapper.selectByUsername(username,password);
        log.debug("uservo="+userVO);
        if(userVO == null ){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("user",userVO);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }
}
